package gui.Reparation;

import gui.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import service.Service;

import model.*;

public class ReparationListFrame extends JFrame {

	private JButton create, update, delete, printer, printerAll, log;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JPanel pSouth;
	private JLabel pic;
	private JTextField search;

	private Controller controller = new Controller();
	protected JTable m_table;
	protected RJTable_StockTableData_old m_data;

	private Service service = Service.getService();

	public ReparationListFrame() {
		setTitle("Reparations");
		setBounds(200, 200, 600, 300);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());

		pSouth = new JPanel(new BorderLayout());
		getContentPane().add(pSouth, BorderLayout.SOUTH);

		getTable();
		getButtons();
		getTextArea();
		getSearch();

		fillListReparation(service.getReparations());
	}

	private void getTable() {
		UIManager.put("Table.focusCellHighlightBorder", new LineBorder(
				Color.black, 0));

		m_data = new RJTable_StockTableData_old();
		m_table = new JTable();
		m_table.addMouseListener(new MouseAdapter() {

			/**
			 * An integer specifying the number of clicks needed to start
			 * editing. Even if <code>clickCountToStart</code> is defined as
			 * zero, it will not initiate until a click occurs.
			 */
			protected int clickCountToStart = 2;

			/**
			 * Specifies the number of clicks needed to start editing.
			 * 
			 * @param count
			 *            an int specifying the number of clicks needed to start
			 *            editing
			 * @see #getClickCountToStart
			 */
			public void setClickCountToStart(int count) {
				clickCountToStart = count;
			}

			/**
			 * Returns the number of clicks needed to start editing.
			 * 
			 * @return the number of clicks needed to start editing
			 */
			public int getClickCountToStart() {
				return clickCountToStart;
			}

			/**
			 * Check the mouseEvent and return true if it is a double click.
			 * 
			 * @author Csaba Tarkanyi
			 */
			public boolean isDoubleClick(EventObject anEvent) {
				if (anEvent instanceof MouseEvent) {
					setClickCountToStart(2);
					return ((MouseEvent) anEvent).getClickCount() >= getClickCountToStart();
				}
				return true;
			}

			public void mousePressed(MouseEvent e) {
				// Left mouse click
				if (SwingUtilities.isLeftMouseButton(e)) {
					if (isDoubleClick(e)) {
						controller.getHistory();
					}
					getDescription(service.getReparations());
				}
				// Right mouse click
				else if (SwingUtilities.isRightMouseButton(e)) {
				}
			}
		});
		m_table.setAutoCreateColumnsFromModel(false);
		m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		m_table.setModel(m_data);
		for (int k = 0; k < m_data.getColumnCount(); k++) {
			DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
			renderer
					.setHorizontalAlignment(RJTable_StockTableData_old.m_columns[k].m_alignment);
			TableColumn column = new TableColumn(k,
					RJTable_StockTableData_old.m_columns[k].m_width, renderer,
					null);
			m_table.addColumn(column);
		}
		JTableHeader header = m_table.getTableHeader();
		header.setUpdateTableInRealTime(false);
		JScrollPane ps = new JScrollPane();
		ps.getViewport().setBackground(m_table.getBackground());
		ps.getViewport().add(m_table);
		getContentPane().add(ps, BorderLayout.CENTER);
	}

	private void getButtons() {
		JPanel pButtons = new JPanel();
		pSouth.add(pButtons, BorderLayout.CENTER);

		create = new JButton("Create");
		create.addActionListener(controller);
		pButtons.add(create);

		update = new JButton("Update");
		update.addActionListener(controller);
		pButtons.add(update);

		delete = new JButton("Delete");
		delete.addActionListener(controller);
		pButtons.add(delete);

		printer = new JButton("Spare Part History");
		printer.addActionListener(controller);
		pButtons.add(printer);

		printerAll = new JButton("All Spare Part History");
		printerAll.addActionListener(controller);
		pButtons.add(printerAll);

		log = new JButton("show log");
		log.addActionListener(controller);
		pButtons.add(log);
	}

	private void getTextArea() {
		textArea = new JTextArea("Descriprion:");
		textArea.setFocusable(true);
		textArea.setEditable(false);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);

		scrollPane = new JScrollPane(textArea);
		pSouth.add(scrollPane, BorderLayout.NORTH);
	}

	private void getSearch() {
		JPanel s = new JPanel(new FlowLayout(FlowLayout.LEADING));
		pic = new JLabel(AppKoopy.createImageIcon("images/search.png"));
		pic.setPreferredSize(new Dimension(20, 22));
		s.add(pic);
		search = new JTextField(" search");
		search.setPreferredSize(new Dimension(160, 20));
		s.add(search);

		pSouth.add(s, BorderLayout.SOUTH);
	}

	private void getDescription(List<Reparation> list) {
		if (m_table.getSelectedRow() == -1) {
			return;
		}
		int index = m_table.getSelectedRow();
		Reparation rep = (Reparation) list.get(index);
		textArea.setText(rep.getDescription());
	}

	/**
	 * Fills the List with Reparations.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void fillListReparation(List<Reparation> list) {
		m_data.setDefaultData(list);
	}

	/**
	 * Fills the List with Reparations.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void fillListReparation() {
		m_data.setDefaultData(service.getReparations());
	}

	private class Controller implements ActionListener {

		private void getHistory() {
			if (m_table.getSelectedRow() == -1) {
				return;
			}
			int index = m_table.getSelectedRow();
			Reparation reparation = (Reparation) service.getReparations().get(
					index);
			PrinterDialog repSPH = new PrinterDialog(
					ReparationListFrame.this,
					"-<<Reparation(" + reparation.getUniqueID() + ") Status>>-",
					reparation.getSparePartHistoryPrint(MainFrame
							.getSimpleDateFormat()));
			repSPH.setVisible(true);
		}

		private void getLog() {
			if (m_table.getSelectedRow() == -1) {
				return;
			}
			int index = m_table.getSelectedRow();
			Reparation reparation = (Reparation) service.getReparations().get(
					index);
			PrinterDialog repSPH = new PrinterDialog(ReparationListFrame.this,
					"-<<Reparation(" + reparation.getUniqueID() + ") Log>>-",
					reparation.getLog());
			repSPH.setVisible(true);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == create) {
				ReparationCUDialog repC = new ReparationCUDialog(
						ReparationListFrame.this, "Create a Repararion");
				// Conference conference =
				// (Conference)listofConferences.getSelectedValue();
				// repC.setConference(conference);
				repC.setVisible(true);
			}

			if (e.getSource() == printer) {
				getHistory();
			}

			if (e.getSource() == printerAll) {
				String allText = "";

				for (Object o : service.getReparations()) {
					Reparation r = (Reparation) o;
					allText += r.getSparePartHistoryPrint(MainFrame
							.getSimpleDateFormat())
							+ "\n";
				}

				PrinterDialog repSPH = new PrinterDialog(
						ReparationListFrame.this, "All SPH from Reparations",
						allText);
				repSPH.setVisible(true);
			}

			if (e.getSource() == log) {
				getLog();
			}
		}
	}
}